//
//  ViewController.m
//  EUCQuickBloxManager
//
//  Created by  ZhuHong on 2016/12/14.
//  Copyright © 2016年 BBDTEK. All rights reserved.
//

#import "ViewController.h"
#import <Quickblox/Quickblox.h>


@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    QBChat* chat = [QBChat instance];
    QBContactList* contactList = chat.contactList;
    
    NSArray* contacts = contactList.contacts;
    
    NSLog(@"%@", contacts);
    
    
    id obj = chat.currentUser;
    
    NSLog(@"%@", obj);

}



//  创建一个新的用户 21672795
- (IBAction)createUser:(id)sender {
    
    QBUUser *user = [QBUUser user];
    user.login = @"garry";
    user.password = @"garry5santos";
    
    [QBRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {
        // Success, do something
        
        NSLog(@"response = %@, user = %@", response, user);
    } errorBlock:^(QBResponse *response) {
        // error handling
        NSLog(@"error: %@", response.error);
    }];
    
}

- (IBAction)loginClick:(id)sender {
    
    [QBRequest logInWithUserLogin:@"garry" password:@"garry5santos" successBlock:^(QBResponse *response, QBUUser *user) {
        // Success, do something
        NSLog(@"response = %@, user = %@", response, user);
    } errorBlock:^(QBResponse *response) {
        // error handling
        NSLog(@"error: %@", response.error);
    }];
    
}
- (IBAction)connetctClick:(id)sender {
    
    QBUUser* currentUser = [[QBUUser alloc] init];
    currentUser.ID = 21672795;
    currentUser.password = @"garry5santos";
    
    QBChat* chat = [QBChat instance];
    {
        QBContactList* contactList = chat.contactList;
        
        id obj = chat.currentUser;
        
        NSLog(@"%@", obj);
        
        NSArray* contacts = contactList.contacts;
        
        NSLog(@"%@", contacts);
    }
    
    [chat connectWithUser:currentUser completion:^(NSError * _Nullable error) {
        NSLog(@"%@", error);
        
        if (error) {
            NSLog(@"链接失败");
        } else {
            NSLog(@"链接成功");
        }
        
        
        // Error Domain=com.quickblox.chat Code=401 "<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/><text lang="en">Password not verified</text></failure>" UserInfo={NSLocalizedDescription=<failure xmlns="urn:ietf:params:xml:ns:xmpp-sasl"><not-authorized/><text lang="en">Password not verified</text></failure>}
        
        // 这个错误:应该是没有这个currentUser
        
    }];
}

// 创建对话列表
- (IBAction)createDiaListClick:(id)sender {
//    21662147, 21665235
    QBChatDialog* chatDialog = [[QBChatDialog alloc] initWithDialogID:@"coder" type:QBChatDialogTypeGroup];
    chatDialog.name = @"第一次";
    chatDialog.occupantIDs = @[@(21662147), @(21665235)];
    
    [QBRequest createDialog:chatDialog successBlock:^(QBResponse * _Nonnull response, QBChatDialog * _Nullable createdDialog) {
        NSLog(@"response = %@, createdDialog= %@", response, createdDialog);
    } errorBlock:^(QBResponse * _Nonnull response) {
        NSLog(@"response = %@", response);
    }];
}

// Session 会话
- (IBAction)SessionClick:(id)sender {
    [QBRequest countOfDialogsWithExtendedRequest:nil successBlock:^(QBResponse * _Nonnull response, NSUInteger count) {
        NSLog(@"response = %@, count = %zd", response, count);
    } errorBlock:^(QBResponse * _Nonnull response) {
        NSLog(@"response = %@", response);
    }];
}

- (IBAction)listClick:(id)sender {
    QBResponsePage* page = [QBResponsePage responsePageWithLimit:100 skip:0];
    
    [QBRequest dialogsForPage:page extendedRequest:nil successBlock:^(QBResponse * _Nonnull response, NSArray<QBChatDialog *> * _Nullable dialogObjects, NSSet<NSNumber *> * _Nullable dialogsUsersIDs, QBResponsePage * _Nullable page) {
        NSLog(@"response = %@, dialogObjects =%@, dialogsUsersIDs= %@, page= %@", response, dialogObjects, dialogsUsersIDs, page);
        
        
//        {(
//          21665235,
//          21672795,
//          21662147
//          )}
    } errorBlock:^(QBResponse * _Nonnull response) {
        NSLog(@"response = %@", response);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
